package com.hospital.billcommon.module.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.billcommon.module.controller.dto.BillDto;
import com.hospital.billcommon.module.controller.dto.RefundBillVo;
import com.hospital.billcommon.module.entity.OutpBillitem;
import com.hospital.billcommon.module.mapper.OutpPriceMapper;
import com.hospital.billcommon.module.service.OutpPriceService;
import com.hospital.common.core.function.PushMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description 门诊数据插入实现类
 * @author: liuwei
 * @create 2020/5/21 5:09 下午
 **/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class OutpPriceServiceImpl extends ServiceImpl<OutpPriceMapper, OutpBillitem> implements OutpPriceService {

    @Resource
    private OutpPriceMapper outpPriceMapper;

    @Resource
    private PushMessage pushMessage;

    @Override
    public boolean insertOutpBillItemList(List<OutpBillitem> list, BillDto billDto, RestTemplate restTemplate) throws Exception {
        String settleFlag = "";
        try {
            settleFlag = billDto.getSettleFlag();
        } catch (NullPointerException e) {
        }
        for (OutpBillitem item : list) {
            try {
                if (settleFlag.equals("1")) {
                    item.setRcptNo(billDto.getSettlePatientInfo().get(0).getRcptNo());
                }
            } catch (NullPointerException e) {
//                throw new RuntimeException("结算操作中的参数无结算单号！");
            }
            try {
                int res = outpPriceMapper.insert(item);
                if (res <= 0) {
                    throw new RuntimeException("插入数据失败");
                }
            } catch (Exception e) {
                if (e.getMessage().contains("ORA-20001")){
                    throw new RuntimeException("插入费用数据异常,材料库存不足或基础数据异常"+ e.getMessage());
                }
                throw new RuntimeException("插入费用数据异常" + e.getMessage());
            }
        }
        //结算接口调用
        if (settleFlag != null && settleFlag.equals("1")) {
            String url = outpPriceMapper.getSettleUrl();
            //结算接口调用开始
            boolean settle = pushMessage.sendBillMsg(JSONObject.toJSONString(billDto), url);
        }
        return true;
    }

    @Override
    public boolean insertOutpRefundBillItemList(List<OutpBillitem> list, RefundBillVo refundBillVo, RestTemplate restTemplate) throws Exception {
        for (OutpBillitem item : list) {
            try {
                int res = outpPriceMapper.insert(item);
                if (res <= 0) {
                    throw new RuntimeException("插入数据失败");
                }
            } catch (Exception e) {
                throw new RuntimeException("退结算接口插入费用数据异常" + e.getMessage());
            }
        }
        //结算接口调用
        pushMessage.sendBillMsg(JSONObject.toJSONString(refundBillVo),outpPriceMapper.getRefundSettleUrl());
        return true;
    }

    @Override
    public Object getMaxItemNo(String visitDate, String visitNo) {
        return outpPriceMapper.getMaxItemNo(visitDate, visitNo);
    }

    @Override
    public String getItemOutpRcptClass(String itemCode, String itemSpec) {
        return outpPriceMapper.getItemOutpRcptClass(itemCode, itemSpec);
    }

    @Override
    public String getSettleUrl() {
        return outpPriceMapper.getSettleUrl();
    }

    @Override
    public String getbillSpec(String visitDate, String visitNo, String itemCode) {
        return outpPriceMapper.getbillSpec(visitDate, visitNo, itemCode);
    }

    @Override
    public long getCount(String itemCode, String type) {
        return outpPriceMapper.getCount(itemCode, type);
    }

    @Override
    public String getVisitNo() {
        return outpPriceMapper.getvisitNo();
    }
}
